package com.zhitong.money.mapper;

import com.zhitong.money.entity.Leave;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface LeaveMapper {

    @Results(id = "leaveMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "tid",property = "tid"),
            @Result(column = "name",property = "name"),
            @Result(column = "pid",property = "pid"),
            @Result(column = "startTime",property = "startTime"),
            @Result(column = "endTime",property = "endTime"),
            @Result(column = "description",property = "description"),
            @Result(column = "leader",property = "leader"),
            @Result(column = "status",property = "status"),
            @Result(column = "result",property = "result"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "selfEnable",property = "selfEnable"),
            @Result(column = "enable",property = "enable")
    })
    @Select("select employee.name,employee.pid,leavetip.* from leavetip,employee where leavetip.tid=employee.tellerId")
    public List<Leave> findAllLeave();

    @ResultMap("leaveMap")
    @Select("select employee.name,employee.pid,leavetip.* from leavetip,employee where leavetip.tid=employee.tellerId and leavetip.id = #{id}  ")
    public Leave findLeaveById(String id);

    @ResultMap("leaveMap")
    @Select({
            "<script>",
            "select employee.name,employee.pid,leavetip.* from leavetip,employee",
            "<where>",
            "and leavetip.tid=employee.tellerId",
            "<if test='leader!=null and leader!=\"\"'>and leader=#{leader}</if>",
            "<if test='tid!=null and tid!=\"\"'>and tid=#{tid}</if>",
            "<if test='result!=null and result!=\"\"'>and result=#{result}</if>",
            "<if test='enable!=null and enable!=\"\"'>and enable=#{enable}</if>",
            "<if test='selfEnable!=null and selfEnable!=\"\"'>and selfEnable=#{selfEnable}</if>",
            "<if test='name!=null and name!=\"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "</where>",
            "</script>"
    })
    public List<Leave> findLeaveByMap(Map<String,Object> map);

    @ResultMap("leaveMap")
    @Select("select employee.name,employee.pid,leavetip.* from leavetip,employee where leavetip.tid=employee.tellerId and result = #{result} and tid=#{tid}")
    public List<Leave> findLeaveByResult(String result,String tid);

    @ResultMap("leaveMap")
    @Select("select employee.name,employee.pid,leavetip.* from leavetip,employee where tid=#{tid} and leavetip.tid=employee.tellerId")
    public List<Leave> findLeaveByTid(String tid);

    @ResultMap("leaveMap")
    @Select("select employee.name,employee.pid,leavetip.* from leavetip,employee where tid = #{tid} and result not in ('03','00') and leavetip.tid=employee.tellerId")
    public List<Leave> findLeaveByNew(String tid);

    @Insert("insert into leavetip values(#{id},#{tid},#{startTime}," +
            "#{endTime},#{description},#{leader},#{status},#{result},Now())")
    public void addLeave(Leave leave);

    @Update({
            "<script>",
            "update leavetip",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='startTime!=null'>startTime=#{startTime},</if>",
            "<if test='endTime!=null'>endTime=#{endTime},</if>",
            "<if test='leader!=null and leader!=\"\"'>leader=#{leader},</if>",
            "<if test='status!=null and status!=\"\"'>status=#{status},</if>",
            "<if test='result!=null and result!=\"\"'>result=#{result},</if>",
            "<if test='description!=null and description!=\"\"'>description=#{description},</if>",
            "<if test='selfEnable!=null and selfEnable!=\"\"'>selfEnable=#{selfEnable},</if>",
            "<if test='enable!=null and enable!=\"\"'>enable=#{enable},</if>",

            "</trim>",
            "where id = #{id}",
            "</script>"
            })
    public void updateLeave(Leave leave);

    @Update("update leavetip set selfEnable = #{selfEnable} where id = #{id}")
    public void updateSelfEnable(String id,String selfEnable);

    @Update("update leavetip set enable = #{enable} where id = #{id}")
    public void updateEnable(String id,String enable);

    @Delete("delete from leavetip where id=#{id}")
    public void delLeave(String id);

    @Delete({
            "<script>",
            "delete from leavetip",
            "where id in ",
            "<foreach item='item' collection='ids' separator=',' open='(' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public void delLeaveByIds(List<String> ids);

}
